/*******************************************************************************
 * Copyright 2009-2015 Amazon Services. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 *
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 * specific language governing permissions and limitations under the License.
 *******************************************************************************
 * Marketplace Web Service Orders
 * API Version: 2013-09-01
 * Library Version: 2015-04-30
 * Generated: Thu May 28 20:14:49 GMT 2015
 */
package com.amazonservices.mws.orders._2013_09_01.samples;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.amazonOrder.get.AmazonOrder;
import com.amazonOrder.get.Order;
import com.amazonOrder.get.Ordertotal;
import com.amazonOrder.get.Shippingaddress;
import com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrders;
import com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersClient;
import com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersException;
import com.amazonservices.mws.orders._2013_09_01.model.GetOrderRequest;
import com.amazonservices.mws.orders._2013_09_01.model.GetOrderResponse;
import com.amazonservices.mws.orders._2013_09_01.model.ResponseHeaderMetadata;
import com.idp.ebay.domain.AmazonInfo;
import com.idp.ebay.utils.ExcelFileUtil;

import cn.creditharmony.loan.util.Xml2JsonUtil;

/** Sample call for GetOrder. */
public class GetOrderSample {

	/**
	 * Call the service, log response and exceptions.
	 *
	 * @param client
	 * @param request
	 *
	 * @return The response.
	 */
	public static GetOrderResponse invokeGetOrder(MarketplaceWebServiceOrders client, GetOrderRequest request) {
		try {
			// Call the service.
			GetOrderResponse response = client.getOrder(request);
			ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();
			// We recommend logging every the request id and timestamp of every
			// call.
			System.out.println("Response:");
			System.out.println("RequestId: " + rhmd.getRequestId());
			System.out.println("Timestamp: " + rhmd.getTimestamp());
			String responseXml = response.toXML();
			String responseStr = response.toString();
			String responseJson = response.toJSON();
			System.out.println(Xml2JsonUtil.xml2JSON(responseXml));
			String jsonstr = Xml2JsonUtil.xml2JSON(responseXml);

			/*
			 * String jsonstr2="["+jsonstr+"]";
			 * 
			 * List<AmazonOrder> al=JSON.parseArray(jsonstr2,
			 * AmazonOrder.class); System.out.println(al);
			 */
			ExcelFileUtil excel = new ExcelFileUtil();
			List<AmazonInfo> ebst = new ArrayList<AmazonInfo>();

			AmazonOrder a2 = JSON.parseObject(jsonstr, AmazonOrder.class);
			Order ord = a2.getGetorderresponse().getGetorderresult().get(0).getOrders().get(0).getOrder().get(0);
			
			
			
			Shippingaddress s = ord.getShippingaddress().get(0);
			String shipAddress = s.toString();

			Ordertotal orderTotals = ord.getOrdertotal().get(0);
			String orderTotal = orderTotals.toString();

			AmazonInfo amazonInfo = new AmazonInfo(ord.getAmazonorderid().get(0), ord.getBuyeremail().get(0),
					ord.getEarliestdeliverydate().toString(), ord.getEarliestshipdate().toString(),
					ord.getSaleschannel().get(0), ord.getOrderstatus().get(0), ord.getNumberofitemsshipped().get(0),
					ord.getOrdertype().get(0), ord.getIspremiumorder().get(0), ord.getIsprime().get(0),
					ord.getBuyername().get(0), ord.getFulfillmentchannel().get(0), ord.getNumberofitemsshipped().get(0),
					ord.getLatestshipdate().toString(), ord.getShippedbyamazontfm().get(0).toString(),
					ord.getShipservicelevel().get(0), ord.getMarketplaceid().get(0),
					ord.getLatestdeliverydate().toString(), ord.getPurchasedate().toString(), shipAddress,
					ord.getPaymentmethod().get(0).toString(), ord.getIsbusinessorder().get(0).toString(), orderTotal,
					ord.getLastupdatedate().toString(), ord.getShipmentservicelevelcategory().get(0).toString());
			System.out.println(ord.toString());

			ebst.add(amazonInfo);

			File file = excel.write("亚马逊订单信息", ebst, AmazonInfo.class,null);
			System.out.println("文件存放路径为：" + file.getPath());

			return response;
		} catch (MarketplaceWebServiceOrdersException ex) {
			// Exception properties are important for diagnostics.
			System.out.println("Service Exception:");
			ResponseHeaderMetadata rhmd = ex.getResponseHeaderMetadata();
			if (rhmd != null) {
				System.out.println("RequestId: " + rhmd.getRequestId());
				System.out.println("Timestamp: " + rhmd.getTimestamp());
			}
			System.out.println("Message: " + ex.getMessage());
			System.out.println("StatusCode: " + ex.getStatusCode());
			System.out.println("ErrorCode: " + ex.getErrorCode());
			System.out.println("ErrorType: " + ex.getErrorType());
			throw ex;
		}
	}

	/**
	 * Command line entry point.
	 */
	public static void main(String[] args) {

		// Get a client connection.
		// Make sure you've set the variables in
		// MarketplaceWebServiceOrdersSampleConfig.
		MarketplaceWebServiceOrdersClient client = MarketplaceWebServiceOrdersSampleConfig.getClient();

		// Create a request.
		GetOrderRequest request = new GetOrderRequest();
		String sellerId = "A31FK1YOBVUT0K";
		request.setSellerId(sellerId);
		String mwsAuthToken = "";
		request.setMWSAuthToken(mwsAuthToken);
		List<String> amazonOrderId = new ArrayList<String>();
		amazonOrderId.add("114-6824236-0625863");
		request.setAmazonOrderId(amazonOrderId);

		// Make the call.
		GetOrderSample.invokeGetOrder(client, request);

	}

}
